//index.js
const app = getApp()
const db = wx.cloud.database();
const _ = db.command;
Page({
  data: {
    active: 0,
    skechPics: [],
    learnPics: []
  },

  // 改变标签页
  onChange(event) {
    console.log(event);
  },

  // 预览线稿
  previewSkech(e) {
    var skechIndex = e.currentTarget.dataset.index;
    var skechPics = this.data.skechPics;
    wx.previewImage({
      urls: skechPics,
      current: skechPics[skechIndex]
    }) 
  },

  previewLearn(e) {
    var learnIndex = e.currentTarget.dataset.index;
    var learnPics = this.data.learnPics;
    wx.previewImage({
      urls: learnPics,
      current: learnPics[learnIndex]
    })
  },

  onLoad: function () {
    var that = this;
    // 获取线稿素材
    db.collection('material').doc('2541245664194196000a7e0d27926792').get({
      success(res) {
        console.log(res)
        // 把得到的两个数组拿到页面渲染
        that.setData({
          skechPics : res.data.skechs,
          learnPics : res.data.learns
        })
      },
      err(err){
        console.log("err" + err);
      }
    })
  },


  // 上传图片
  doUpload: function () {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]

        // 上传图片
        const cloudPath = `my-image${filePath.match(/\.[^.]+?$/)[0]}`
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)

            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },

})